import pathlib
from re import L
from peewee import *

from enum import IntEnum, unique

class QuestionType(IntEnum):
    A=1
    X =2


db_path=pathlib.Path(__file__).parent.parent/'tiku.sqlite'

db=SqliteDatabase(db_path)

class BaseModel(Model):
    class Meta:
        database=db

class Chapter(Model):
    class Meta:
        database=db
        table_name='SECTION'
    id=IntegerField(column_name='PART_ID',primary_key=True)
    parent=ForeignKeyField('self',column_name='PARENT_ID',backref='chapters')
    title=TextField(column_name='TITLE')
    is_xueshuo=BooleanField(column_name='AM')
    is_zhuanshuo=BooleanField(column_name='PM')

    @property
    def question_count(self):
        if self.chapters:
            count=0
            for chapter in self.chapters:
                count+=chapter.question_count
            return count
        else:
            return Question.select().where(Question.chapter==self).count()

class Question(Model):
    class Meta:
        database=db
        table_name='QUESTION'
    id=IntegerField(column_name='QUESTION_ID',primary_key=True)
    title=TextField(column_name='TITLE')
    number=TextField(column_name='NUMBER')
    order=IntegerField(column_name='NUMBER_NUMBER')
    year=IntegerField(column_name='YEAR')
    restore=TextField(column_name='RESTORE')
    explain=TextField(column_name='EXPLAIN')
    question_type=IntegerField(column_name='TYPE')
    answer=TextField(column_name='ANSWER')
    subject_name=TextField(column_name='SUBJECT_NAME')
    xueshuo_dagang=TextField(column_name='XUEHSUODAGANG',null=True)
    zhuanshuo_dagang=TextField(column_name='ZHUANSHUODAGANG',null=True)
    is_xueshuo=BooleanField(column_name='ISXUESHUO')
    is_zhuanshuo=BooleanField(column_name='ISZHUANSHUO')
    is_hot=BooleanField(column_name='ISGAOPINKAODIAN')
    chapter=ForeignKeyField(Chapter,backref='questions',column_name='CHAPTER_ID')
    subject=ForeignKeyField(Chapter,column_name='CHAPTER_PARENT_ID')
    source=TextField(column_name='SOURCE',null=True)

    @property
    def options(self):
        return list(self.Options)
    
    @property
    def chapter_id(self):
        return self.chapter.id
    
    @property
    def answer_sheet(self):
        return self.Answer_sheet[0]


class Option(Model):
    class Meta:
        database=db
        table_name='OPTION'
        primary_key=False
    question=ForeignKeyField(Question,column_name='QUESTION_ID',backref='Options')
    value=TextField(column_name='VALUE')
    key=TextField(column_name='KEY')


class SheetItem(Model):
    class Meta:
        database=db
        table_name='ANSWER'
    question=ForeignKeyField(Question,backref='Answer_sheet',unique=True)
    check_count=IntegerField(default=0)
    right_count=IntegerField(default=0)
    last_check_right=BooleanField(default=0)

    @property
    def checked(self):
        return self.check_count>0
    
    @property
    def accuracy(self):
        return self.right_count/self.check_count if self.check_count else 0

    @property
    def question_id(self):
        return self.question.id

    @property
    def order(self):
        return self.question.order

    @property
    def qtype(self):
        t=self.question.question_type
        if t==QuestionType.A:
            return 'A'
        if t==QuestionType.X:
            return 'X'